package com.aidl.demo.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aidl.demo.R;
import com.aidl.demo.imageloader.GlideImageLoader;
import com.aidl.demo.utils.TimeUtils;
import com.aidl.demo.utils.Utils;
import com.youth.banner.Banner;
import com.youth.banner.transformer.BackgroundToForegroundTransformer;
import com.youth.banner.transformer.ForegroundToBackgroundTransformer;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {

    @BindView(R.id.tv_welcome_guest)
    TextView mTvWelcomeGuest;
    @BindView(R.id.tv_time)
    TextView mTvTime;
    @BindView(R.id.img_show)
    Banner mImgShow;
    @BindView(R.id.img_weather)
    Banner mImgWeather;
    @BindView(R.id.img_ad)
    Banner mImgAd;
    @BindView(R.id.tv_live)
    TextView mTvLive;
    @BindView(R.id.tv_look_back)
    TextView mTvLookBack;
    @BindView(R.id.tv_vod)
    TextView mTvVod;
    @BindView(R.id.tv_language)
    TextView mTvLanguage;
    @BindView(R.id.tv_setting)
    TextView mTvSetting;
    @BindView(R.id.tv_mac)
    AppCompatTextView mTvMac;
    @BindView(R.id.tv_message)
    AppCompatTextView mTvMessage;
    @BindView(R.id.img_large)
    ImageView mImgLarge;
    @BindView(R.id.tv_show)
    TextView mTvShow;

    @Override
    protected int getContentResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        mTvLive.requestFocus();
        mTvMac.setText("MAC:" + Utils.getMac(this));
        loadDefaultImg();
    }

    @Override
    protected void initData() {
        mTimer.schedule(mTimerTask, 0, 1000);
    }

    @OnClick({R.id.tv_live, R.id.tv_look_back, R.id.tv_vod, R.id.tv_language, R.id.tv_setting,
            R.id.tv_show})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_live:
                startActivity(new Intent(MainActivity.this, LiveActivity.class));
                break;
            case R.id.tv_look_back:
                break;
            case R.id.tv_vod:
                startActivity(new Intent(MainActivity.this, MovieActivivty.class));
                break;
            case R.id.tv_language:
                break;
            case R.id.tv_show:
                startActivity(new Intent(MainActivity.this, ShowActivity.class));
                break;
            case R.id.tv_setting:
                break;
        }
    }

    private List<Object> imgUrls1;
    private List<Object> imgUrls2;
    private List<Object> imgUrls3;

    //加载默认图片
    private void loadDefaultImg() {
        imgUrls1 = new ArrayList<>();
        imgUrls2 = new ArrayList<>();
        imgUrls3 = new ArrayList<>();
        imgUrls1.add(getResources().getDrawable(R.drawable.wooden_house));
        mImgShow.setImages(imgUrls1)
                .setPageTransformer(true, new BackgroundToForegroundTransformer())
                .setImageLoader(new GlideImageLoader())
                .start();
        imgUrls2.add(getResources().getDrawable(R.drawable.weather));
        mImgWeather.setImages(imgUrls2)
                .setPageTransformer(true, new ForegroundToBackgroundTransformer())
                .setImageLoader(new GlideImageLoader())
                .start();
        imgUrls3.add(getResources().getDrawable(R.drawable.sence));
        mImgAd.setImages(imgUrls3)
                .setImageLoader(new GlideImageLoader())
                .start();
    }

    //时钟
    private Timer mTimer = new Timer(true);

    private TimerTask mTimerTask = new TimerTask() {
        @Override
        public void run() {
            mHandler.sendEmptyMessage(UPDATE_TIME);
        }
    };

    public static final int UPDATE_TIME = 0;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case UPDATE_TIME:
                    String date = TimeUtils.stampToDate(System.currentTimeMillis());
                    String str_dayofWeek = getResources()
                            .getStringArray(R.array.day_of_week)[TimeUtils.getDay(System.currentTimeMillis()) - 1];
                    mTvTime.setText(date + "  " + str_dayofWeek);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
